<?php
/**
 * Created by JetBrains PhpStorm.
 * User: andrei
 * Date: 7/31/12
 * Time: 1:37 PM
 * To change this template use File | Settings | File Templates.
 */

class Db_Cms_Menus extends Common {
	protected static $tableName = TAB_MENUS;
	protected static $dbFields = array(
		'id',
		'm_project_identifier',
		'm_title',
		'm_identifier',
		'm_template',
		'deleted'
	);
    /**
     * @var Db_Cms_Menus
     */
    public $objectArray;

	public $id;
	public $m_project_identifier;
	public $m_title;
	public $m_identifier;
	public $m_template;
	public $deleted;

	function __construct($value = NULL, $fieldName = NULL) {
		parent::__construct($value, $fieldName);
	}

	/**
	 * Returns all the "visible" items for a menu identifier in the current loaded project
	 *
	 * @static
	 * @param string $identifier
	 * @return bool|mixed
	 */
	public static function getItems($identifier) {
		if (!empty($identifier)) {
			return self::$DB->select(
				"SELECT p.id, p.p_url, m.m_template, mp.mp_item_title, mp.mp_item_icon, mp.mp_parent_id, mp.mp_position,mp.mp_menu_id, mp.id as menu_page_id,pm.pm_identifier
					FROM ?f m
					JOIN ?f mp ON m.id = mp.mp_menu_id AND mp.mp_display = 1
					JOIN ?f p ON p.id = mp.mp_page_id AND p.p_404_error = 0 AND p.`deleted` = 0
					LEFT JOIN ?f pm ON p.p_permission_id = pm.id
	            WHERE m.`deleted` = 0 AND `mp`.`deleted` = 0 AND m.`m_identifier` = ? AND m.m_project_identifier = ? ORDER BY `mp`.`mp_position` ASC;",
				self::$tableName, TAB_MENU_PAGES, TAB_PAGES, TAB_PERMISSIONS, $identifier, self::$PROJECT['pr_identifier']);
		}
		return false;
	}

	/**
	 * Returns all pages belonging to a menu ID. Used for menu items configuration
	 *
	 * @static
	 * @param $menuId
	 * @return bool|mixed
	 */
	public static function getItemsById($menuId) {
		if ((int)$menuId > 0) {
			return self::$DB->select(
				"SELECT p.id, p.p_url, mp.mp_item_title, mp.mp_item_icon, mp.mp_display, mp.mp_parent_id, mp.mp_position,mp.mp_menu_id,
						mp.id as menu_page_id
					FROM ?f m
					JOIN ?f mp ON mp.mp_menu_id = m.id
					JOIN ?f p ON p.id = mp.mp_page_id AND p.deleted = 0
				WHERE m.deleted = 0 AND `mp`.`deleted` = 0 AND m.id = ?i
				ORDER BY mp.mp_position ASC;",
				self::$tableName, TAB_MENU_PAGES, TAB_PAGES, $menuId
			);
		}
		return false;
	}

	public static function getPageByName($menuId, $parentId, $name) {
		return self::$DB->selectRow("SELECT * FROM ?f WHERE `mp_menu_id` = ?d
                 AND `mp_parent_id` = ?d AND `mp_item_title` = ?", self::$tableName, $menuId, $parentId, $name);
	}

	/**
	 * This function gets all Menus, fields (Id, Name) and all pages which belong each menu
	 * @static
	 * @return array
	 */
	public static function getMenus() {
		$sql = "SELECT `m`.`id` as `mp_menu_id`, `m`.`m_title`, `p`.`id` as `pageId` FROM ?f  m
		LEFT JOIN ?f mp ON `mp`.`deleted` = 0 AND `mp`.`mp_menu_id` = `m`.`id`
		LEFT JOIN ?f p ON `p`.`deleted` = 0 AND `mp`.`mp_page_id` = `p`.`id`
		WHERE `m`.`deleted` = 0 ";
		$menuPages = self::$DB->q(true, $sql, TAB_MENUS, TAB_MENU_PAGES, TAB_PAGES);
		$pagesId = array();
		$dataReturn = array();
		$pagesListId = Db_Cms_Pages::findAllArray("id");
		$pageIdArray = array();
		if ($pagesListId) {
			foreach ($pagesListId as $pageId) {
				$pageIdArray[] = $pageId['id'];
			}
		}
		while ($data = $menuPages->fetch_assoc()) {
			if (isset($data['pageId']) && in_array($data['pageId'], $pageIdArray)){
				$pagesId[$data['mp_menu_id']][] = "pag".$data['pageId'];
			}
			if (!isset($pagesId[$data['mp_menu_id']])) {
				$pagesId[$data['mp_menu_id']] = array();
			}
			$countMenuPages = count($pagesId[$data['mp_menu_id']]);
			$dataReturn[$data['mp_menu_id']] = array(
				"id" => "menu".$data['mp_menu_id'],
				"name" => t(T_MENU_TITLE, $data['m_title'])." (".$countMenuPages.")",
				"pages" => $pagesId[$data['mp_menu_id']]
			);
		}
		return array_values($dataReturn);
	}

	/**
	 * This function return all menus Pages
	 *
	 * @static
	 * @return array
	 */
	public static function getMenusPages() {
		$sql = 'SELECT `m`.*, `mp`.`mp_page_id` FROM ?f AS `m`
				INNER JOIN ?f AS `mp` ON `m`.`id` = `mp`.`mp_menu_id`
				WHERE `m`.`deleted` = 0 AND `mp`.`deleted` = 0;';
		$menus = self::$DB->q($sql, self::$tableName, TAB_MENU_PAGES);
		$resultMenus = array();
		while($data = $menus->fetch_assoc()) {
			$resultMenus[$data['mp_page_id']][$data['m_project_identifier']][] = $data['m_title'];
		}
		return $resultMenus;
	}



}